package hu.veilingsite.gui;

import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;
import hu.veilingsite.client.Systeem;
import hu.veilingsite.shared.ClientUser;
import hu.veilingsite.shared.FieldVerifier;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Profile {
	private Label lblName,lbrName,lblAddress,lbrAddress,lblResidence,lbrResidence,lblBirthDate,lbrBirthDate,lblEmail,lbrEmail,lblCredits,lbrCredits,lblMayOffer,lbrMayOffer,lblMayBid,lbrMayBid;
	private ClientUser user = Systeem.ingelogd;
	final ServiceAsync myService = (ServiceAsync)GWT.create(Service.class);

	public Profile() {
	}
	
	final AsyncCallback<ClientUser> getUser = new AsyncCallback<ClientUser>(){

		@Override
		public void onFailure(Throwable caught) {
			FieldVerifier.showMessageBox("Oops", "Uw credits zijn niet goed geupdate. log opnieuw in om dit recht te zetten", true);
		}

		@Override
		public void onSuccess(ClientUser result) {
			Systeem.ingelogd = result;
			user = result;
			
			//tabels
			FlexTable links = new FlexTable();
			FlexTable labels = new FlexTable();
			//Panel
			VerticalPanel p = new VerticalPanel();
			//Labels
			lblName = new Label("Naam");
			lbrName = new Label();
			lblAddress = new Label("Adres");
			lbrAddress = new Label();
			lblResidence = new Label("Woonplaats");
			lbrResidence = new Label();
			lblBirthDate = new Label("Geboorte Datum");
			lbrBirthDate = new Label();
			lblEmail = new Label("Email");
			lbrEmail = new Label();
			lblCredits = new Label("Credits");
			lbrCredits = new Label();
			lblMayOffer = new Label("Mag veilen");
			lbrMayOffer = new Label();
			lblMayBid = new Label("Mag bieden");
			lbrMayBid = new Label();
			//adding data
			lbrName.setText(user.getName());
			lbrAddress.setText(user.getAddress());
			lbrResidence.setText(user.getResidence());
			lbrBirthDate.setText(user.getBirthdate().toString());
			lbrEmail.setText(user.getEmail());
			lbrCredits.setText(user.getCredits()+" Credits");
			if (user.isMayoffer()) {
				lbrMayOffer.setText("Ja");
			} else {
				lbrMayOffer.setText("Nee");
			}
			if (user.isMaybid()) {
				lbrMayBid.setText("Ja");
			} else {
				lbrMayBid.setText("Nee");
			}
			//adding to panels
			links.setWidget(0, 0, new HTML("<a href=\"#editProfile\">Bewerk Profiel</a>"));
			labels.setWidget(0, 0, lblName);
			labels.setWidget(1, 0, lblAddress);
			labels.setWidget(2, 0, lblResidence);
			labels.setWidget(3, 0, lblBirthDate);
			labels.setWidget(4, 0, lblEmail);
			labels.setWidget(5, 0, lblCredits);
			labels.setWidget(6, 0, lblMayOffer);
			labels.setWidget(7, 0, lblMayBid);

			links.setWidget(0, 1, new HTML("<a href=\"#profile\">Profiel</a>"));
			labels.setWidget(0, 1, lbrName);
			labels.setWidget(1, 1, lbrAddress);
			labels.setWidget(2, 1, lbrResidence);
			labels.setWidget(3, 1, lbrBirthDate);
			labels.setWidget(4, 1, lbrEmail);
			labels.setWidget(5, 1, lbrCredits);
			labels.setWidget(6, 1, lbrMayOffer);
			labels.setWidget(7, 1, lbrMayBid);
			links.setWidget(0, 2, new HTML("<a href=\"#profileAuctions\">Uw veilingen</a>"));
			links.setWidget(0, 3, new HTML("<a href=\"#profileBids\">Uw biedingen</a>"));
			
			//completing panels
			p.add(links);
			p.add(labels);
			//toevoegen van de adminopties als je admin bent
			if(user.isIsadmin()){
				links.setWidget(0, 4, new HTML("<a href=\"#profileAdminUsers\">Gebruikers Beheren</a>"));
				links.setWidget(0, 5, new HTML("<a href=\"#profileAdminAuctions\">Veilingen Beheren</a>"));
			}
			
			//add all to main
			RootPanel.get("main").clear();
			RootPanel.get("main").add(p);
		}
	};

	public Panel createPage() {
		//user opnieuw ophalen
		myService.getUser(Systeem.ingelogd.getEmail(), getUser);
		//leeg panel returnen om de historyListener tevreden te houden
		VerticalPanel p = new VerticalPanel();
		return p;
	}
}
